﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;

namespace QLKH.PopUp.Hoso
{
    public partial class popKihopdong : Form
    {
        private Int64 id = -1;
        private Int16 idqh = -1;
        public popKihopdong(Int64 cusid,Int16 qhid)
        {
            InitializeComponent();
            id = cusid;
            idqh = qhid;
        }

        private void binCombo(ComboBox cb, DataTable dt, string name, string value, string option)
        {
            List<trangthai> list = new List<trangthai>();
            trangthai ls = null;
            ls = new trangthai();
            ls.name = option;
            ls.value = "0";
            list.Add(ls);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ls = new trangthai();
                    ls.name = dt.Rows[i][name].ToString();
                    ls.value = dt.Rows[i][value].ToString();
                    list.Add(ls);
                }
            }
            cb.DataSource = list;
            cb.DisplayMember = "name";
            cb.ValueMember = "value";
        }

        private void popKihopdong_Load(object sender, EventArgs e)
        {
            //gia
            clTengia cltg = new clTengia();
            DataTable dttg = cltg.getdata(idqh);
            binCombo(cbgia, dttg, "ten", "id", "<Chọn giá tính>");
            //khuyen mai
            clKM km = new clKM();
            DataTable dtkm = km.getdata(1);
            binCombo(cbkhuyenmai, dtkm, "ten", "id", "<Chọn khuyến mãi>");
            //auto khach hang
            clKhachhang kh = new clKhachhang();
            AutoCompleteStringCollection dskhachhang = new AutoCompleteStringCollection();
            DataTable dtds = kh.getautocomplete(0, 0);
            if (dtds != null && dtds.Rows.Count > 0)
            {
                foreach (DataRow row in dtds.Rows)
                {
                    dskhachhang.Add(row["soHD"].ToString());
                }
            }
            txtshd.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txtshd.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtshd.AutoCompleteCustomSource = dskhachhang;
        }

        // khai báo 1 hàm delegate
        public delegate void UpdateCus(int id);
        // khai báo 1 kiểu hàm delegate
        public UpdateCus MyGetData;

        private void btupdate_Click(object sender, EventArgs e)
        {
            if (MyGetData != null)
            {// tại đây gọi nó
                // ki hop dong
                string sohopdong = txtshd.Text.Trim();
                Int16 idKM = Int16.Parse((cbkhuyenmai.SelectedValue == null) ? "0" : cbkhuyenmai.SelectedValue.ToString().Trim());
                string tenKM = (cbkhuyenmai.SelectedValue == null || cbkhuyenmai.SelectedValue.ToString() == "0") ? "" : cbkhuyenmai.Text.Trim();
                Int16 phi = Int16.Parse((cbphi.Checked == true) ? "1" : "0");
                Int16 giaid = Int16.Parse(cbgia.SelectedValue.ToString().Trim());
                Int64 ngayki = Int64.Parse(date.Text.Trim().Split('/')[2].ToString() + date.Text.Trim().Split('/')[1].ToString() + date.Text.Trim().Split('/')[0].ToString());
                Int16 trangthai = 2;
                clKhachhang clkh = new clKhachhang();
                int outid = clkh.kihopdong(id, sohopdong, idKM, tenKM, phi, giaid, ngayki, trangthai);
                MyGetData(outid);
            }
            this.Close();
        }

        private void btquit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
